package com.young.ums.web;

import com.young.common.client.ums.UmsClientHelper;
import com.young.common.util.Result;
import com.young.interfaces.log.annotation.Log;
import com.young.ums.model.AuthorityParameter;
import com.young.ums.model.User;
import com.young.ums.model.Dept;
import com.young.ums.model.Role;
import com.young.ums.service.IDataAuthService;
import com.young.ums.service.IDeptService;
import com.young.ums.service.IRoleService;
import com.young.ums.service.IUserService;
import com.young.ums.util.AuthHelper;
import com.young.ums.util.UmsConstants;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 获取树控制器
 * @author imrookie
 * @date 2018/10/9
 */
@Controller
@RequestMapping("/ums/tree")
public class TreeController {

    @Resource(name="deptService")
    IDeptService deptService;

    @Resource(name="roleService")
    IRoleService roleService;

    @Resource(name="userService")
    IUserService userService;

    @Log("获取单位角色树")
    @RequestMapping("/getRoleTree")
    @ResponseBody
    public Object getRoleTree(Dept dept, HttpServletRequest request) {
        AuthHelper.setAuthWhenEnableAuthIsNull(dept);//权限启用参数没有传时默认启用权限设置
        dept.setType(UmsConstants.DICTIONARY.DEPT_TYPE_COMPANY);//单位查询
        List<Dept> list = deptService.getList(dept);
        if (list != null && list.size() > 0){
            for (Dept dept1 : list){
                List<Role> roles = roleService.getListByCompanyId(dept1.getId());
                dept1.setRoleList(roles);
            }
        }
        return Result.buildSuccess(list);
    }

    @Log("获取单位用户树")
    @RequestMapping("/getCompanyUserTree")
    @ResponseBody
    public Object getCompanyUserTree(Dept dept, HttpServletRequest request) {
        AuthHelper.setAuthWhenEnableAuthIsNull(dept);//权限启用参数没有传时默认启用权限设置
        dept.setType(UmsConstants.DICTIONARY.DEPT_TYPE_COMPANY);//单位查询
        List<Dept> list = deptService.getList(dept);
        if (list != null && list.size() > 0){
            for (Dept dept1 : list){
                List<User> users = userService.getListByCompanyId(dept1.getId());
                dept1.setUserList(users);
            }
        }
        return Result.buildSuccess(list);
    }

    //todo 权限还没加

    @Log("获取单位树")
    @RequestMapping("/getCompanyTree")
    @ResponseBody
    public Object getCompanyTree(Dept dept, HttpServletRequest request) {
        AuthHelper.setAuthWhenEnableAuthIsNull(dept);//权限启用参数没有传时默认启用权限设置
        dept.setType(UmsConstants.DICTIONARY.DEPT_TYPE_COMPANY);//单位查询
        List<Dept> list = deptService.getList(dept);
        return Result.buildSuccess(list);
    }

    @Log("获取部门树")
    @RequestMapping("/getDeptTree")
    @ResponseBody
    public Object getDeptTree(Dept dept, HttpServletRequest request) {
        AuthHelper.setAuthWhenEnableAuthIsNull(dept);//权限启用参数没有传时默认启用权限设置
        dept.setType(UmsConstants.DICTIONARY.DEPT_TYPE_DEPT);//单位查询
        List<Dept> list = deptService.getList(dept);
        return Result.buildSuccess(list);
    }

    @Log("获取组织机构树")
    @RequestMapping("/getOrgTree")
    @ResponseBody
    public Object getOrgTree(Dept dept, HttpServletRequest request) {
        AuthHelper.setAuthWhenEnableAuthIsNull(dept);//权限启用参数没有传时默认启用权限设置
        List<Dept> list = deptService.getList(dept);
        return Result.buildSuccess(list);
    }
}
